package com.vastcom.cms.service.impl;

import com.vastcom.cms.dao.CarouselDao;
import com.vastcom.cms.dao.NewsDao;
import com.vastcom.cms.dao.SinglepageDao;
import com.vastcom.cms.dao.UsersDao;
import com.vastcom.cms.model.Carousel;
import com.vastcom.cms.model.News;
import com.vastcom.cms.model.Singlepage;
import com.vastcom.cms.model.Users;
import com.vastcom.cms.service.ManageService;
import com.vastcom.cms.tools.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

@Service
public class ManageServiceImpl implements ManageService {
    @Autowired
    private NewsDao newsDao;
    @Autowired
    private CarouselDao carouselDao;

    @Autowired
    private UsersDao usersDao;

    @Autowired
    private SinglepageDao singlepageDao;


    @Override
    public Singlepage saveSinglepage(Singlepage singlepage) {

        try {
            return singlepageDao.save(singlepage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean deleteSinglepage(String id) {
        try {
            singlepageDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }



    @Override
    public Singlepage getSinglepage(String id) {
        Optional<Singlepage> temp=singlepageDao.findById(id);
        return temp.isPresent()?temp.get():null;
    }

    @Override
    public List<Singlepage> getSinglepageList() {
        return singlepageDao.findAll();
    }





    @Override
    public News saveNews(News news) {

        try {
            return newsDao.save(news);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    @Override
    public boolean deleteNews(String id) {
        try {
            newsDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public News geiNewsById(String id) {
        Optional<News> temp=newsDao.findById(id);
        return temp.isPresent()?temp.get():null;
    }

    @Override
    public List<News> getNewsList() {
        return newsDao.findAll();
    }
    @Override
    public Page<News>getNewsForPage(int page,int pagesize){
        return newsDao.getNewsForPage(PageRequest.of(page,pagesize));
    }
     @Override
    public Page<News> getNewsByPage(String cate,int page,int pagesize){
              return newsDao.getNewsListByCate(cate,PageRequest.of(page,pagesize));
     }

    @Override
    public Carousel saveCarousel(Carousel carousel) {
        try {
            return carouselDao.save(carousel);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean deleteCarousel(String id) {
        try {
            carouselDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Carousel getCarousel(String id) {
        Optional<Carousel>temp=carouselDao.findById(id);
        return temp.isPresent()?temp.get():null;
    }

    @Override
    public List<Carousel> getCarouselList() {
        return carouselDao.findAll();
    }


    //接口实现
    //获取当前登录用的的Id
    @Override
    public String getCurrentUserId() {
        String userid= (String) SecurityContextHolder.getContext().getAuthentication() .getPrincipal();
        if(Tools.isNullOrSpace(userid)){
            return null;
        }
        else {
            return userid;
        }
    }
    //获取当前登录用户的角色
    @Override
    public String getCurrentRole() {
        String role=null;
        Collection<SimpleGrantedAuthority> authorities = (Collection<SimpleGrantedAuthority>)    SecurityContextHolder.getContext().getAuthentication().getAuthorities();
        for (GrantedAuthority authority : authorities) {
            role = authority.getAuthority();

        }

        if(Tools.isNullOrSpace(role)){
            return null;
        }
        else{
            return role;
        }
    }

    @Override
    public Users saveUser(Users u) {
        try {
            return usersDao.save(u);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean deleteUser(String id) {
        try {
            usersDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Users getUser(String id) {
        Optional<Users> temp=usersDao.findById(id);
        return temp.isPresent()?temp.get(): null;
    }

    @Override
    public Users getUserByUserNameAndPass(String username, String pass) {
        List<Users> ul=usersDao.getUsersByUsernameAndAndPass(username,pass);
        if(ul.size()>0){
            return ul.get(0);
        }
        return null;
    }

    @Override
    public boolean isExsistUserName(String username) {
        List<Users> ul=usersDao.getUsersByUsername(username);
        if(ul.size()>0){
            return true;
        }
        return false;
    }
}
